package leetcode;


/**
 * @author chengz
 * @description 数字转成中文
 * @create 2020-11-13 17:06
 */
public class NumToChinese {
    String point = "点";
    String[] numArr = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    String[] unitArr = {"个", "十", "百", "千", "万"};
    /**
     * 数字转成整数
     * @param input 整数部分
     */
    public void test2(String input) {
        StringBuffer sb = new StringBuffer();
        //根据点的部分，分成两部分
        if(input.contains(".")){
            String[] split = input.split("\\.");
            toIntChinese(split[0],sb);
            toFloatChinese(split[1],sb);
        }else{
            toIntChinese(input, sb);
        }


        System.out.println(sb.toString());
    }

    private void toFloatChinese(String s, StringBuffer sb) {
        sb.append("点");
        for (int i = 0; i < s.length(); i++) {
            int num = Integer.parseInt(s.charAt(i) + "");
            sb.append(numArr[num]);
        }
    }

    private void toIntChinese(String input,  StringBuffer sb) {
        int size = input.length();//4
        for (int i = 0; i < input.length(); i++) {
            int num = Integer.parseInt(input.charAt(i) + "");
            if ((i + 1) != size) {
                sb.append(numArr[num]);
                if (num != 0) {
                    if (i < size) {
                        sb.append(unitArr[size - i - 1]);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        new NumToChinese().test2("2120.22");
    }
}
